<?php 
## include required files
/*******************************/
require_once '../model/trade.php';
require_once '../model/moduleuser.php';
require_once '../model/leads.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$tradeObj = new Model_TRADE();
$moduleObj=new Model_ModuleUser();
$leadObj=new Model_Leads();

/*******************************/

function order_array_num ($array, $key, $order = "ASC")
    {
        $tmp = array();
        foreach($array as $akey => $array2)
        {
            $tmp[$akey] = $array2[$key];
        }
       
        if($order == "DESC")
        {arsort($tmp , SORT_NUMERIC );}
        else
        {asort($tmp , SORT_NUMERIC );}

        $tmp2 = array();       
        foreach($tmp as $key => $value)
        {
            $tmp2[$key] = $array[$key];
        }       
       
        return $tmp2;
    } 

## Active, Inactive selected records
/*******************************/
if(isset($_GET['pid']) != '')
{
		if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
				$ids = @implode(",", $_POST['checkall']);
				## check which action is selected
				$hidden_page = $_POST['hidden_pageno'];	
				if($_POST['action'] == 'active') {
					## Active selected records
					$tradeObj->updateTradeCategoryStatus($ids, '1');
					$_SESSION['msg']="<div class='success_msg'><span>Job Type(s) status activated successfully</span></div>";
				} else if($_POST['action'] == 'inactive') {
					## Inactive selected records
					$tradeObj->updateTradeCategoryStatus($ids, '0');
					$_SESSION['msg']="<div class='success_msg'><span>Job Type(s) status deactivated successfully</span></div>";
				} else {
					## Delete selected records
					$tradeObj->deleteJobTypeCategory($ids);
					$_SESSION['msg']="<div class='success_msg'><span>Job Type(s) deleted successfully</span></div>";
				}
				header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&pid='.$_GET['pid'].'&page='.$hidden_page);
					exit;
				## Delete code will be here if required
				
		} // if close
}
else
{
		if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
				$ids = @implode(",", $_POST['checkall']);
				## check which action is selected
				$hidden_page = $_POST['hidden_pageno'];	
				if($_POST['action'] == 'active') {
					## Active selected records
					$tradeObj->updateTradeCategoryStatus($ids, '1');
					$_SESSION['msg']="<div class='success_msg'><span>Trade(s) category status activated successfully</span></div>";
				} else if($_POST['action'] == 'inactive') {
					## Inactive selected records
					$tradeObj->updateTradeCategoryStatus($ids, '0');
					$_SESSION['msg']="<div class='success_msg'><span>Trade(s) category status deactivated successfully</span></div>";
				} else {
					## Delete selected records					
					$tradeObj->deleteTradeCategory($ids);
					$_SESSION['msg']="<div class='success_msg'><span>Trade(s) category deleted successfully</span></div>";
				}
				header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&page='.$hidden_page);
					exit;
				## Delete code will be here if required
				
		} // if close
	
}
## Active/Inactive/Delete thourgh URL
if(isset($_GET['action']) == 'status' && isset($_GET['status']) != '') {
	$id = base64_decode($_GET['id']);
	## Update cms page status 
	$array = array();
	$array['status'] = ($_GET['status']=='1'?0:1);
	$array['description'] = $_POST['description']; 
	$tradeObj->editTradeCategoryById($array, $id);
	if(isset($_GET['pid']) != '')
	{
		if($_GET['status']=='1')
			$_SESSION['msg']="<div class='success_msg'><span>Job Type status deactivated successfully</span></div>";
		else
			$_SESSION['msg']="<div class='success_msg'><span>Job Type status activated successfully</span></div>";	
		header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&pid='.$_GET['pid'].'&page='.$_GET['page']);
	}
	else
	{
		if($_GET['status']=='1')
			$_SESSION['msg']="<div class='success_msg'><span>Trade category status deactivated successfully</span></div>";
		else
			$_SESSION['msg']="<div class='success_msg'><span>Trade category status activated successfully</span></div>";	
		header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&page='.$_GET['page']);
	
	}
	exit;
}

if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);
	## Delete TRADE category	
	if(isset($_GET['pid']) != '')
	{
		$tradeObj->deleteJobTypeById($id);
		$_SESSION['msg']="<div class='success_msg'><span>Job Type deleted successfully</span></div>";
		header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&pid='.$_GET['pid'].'&page='.$_GET['page']);
	}
	else
	{
		$tradeObj->deleteTradeCategoryById($id);
		$_SESSION['msg']="<div class='success_msg'><span>Trade category deleted successfully</span></div>";
		header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&page='.$_GET['page']);
	}
	exit;
}
if($_GET['q']=='tradecategories')
{
	$moduelArray=$moduleObj->getModuleHelpText(8);
	$smarty->assign('moduelArray', $moduelArray);
	//echo '<pre>';print_r($moduelArray);exit;
}
if($_GET['pid']!='')
{
	$pid= base64_decode($_GET['pid']); 
	$parentArray=$tradeObj->getTradeCategoryDetailsById($pid);
	$smarty->assign('parentArray', $parentArray);
	//echo '<pre>';print_r($moduelArray);exit;
}
/*******************************/
##Fetch all cms pages from database
/******************************/
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search') {
	$searchindex = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}
$searchindex=trim($searchindex);
// Assign search variable to show in search textbox
$smarty->assign('search', stripslashes (stripslashes ($searchindex)));

if($_GET['pid'] != '') {
	$pid = base64_decode($_GET['pid']);
} else {
	$pid = '0';
}

if($_GET['pid'] == '') 
{
	if($_GET['col1'] != '') 
	{
		$col1 = base64_decode($_GET['col1']);
	} 
	else if($_GET['col2'] != '') 
	{
		$col2 = base64_decode($_GET['col2']);
	} 	
	else 
	{
		$col1 = '0';	
	}

}
else
{
	if($_GET['col1'] != '') {
		$col1 = base64_decode($_GET['col1']);
	} else {
		$col1 = '0';
	}

}

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	if($_GET['col1'] != '') 
	{
		if($_GET['pid'] != '') {
			$pageName = "home.php?q=tradecategories&pid=".$_GET['pid']."&col1=".$_GET['col1'].""; 
		} else {
			$pageName = "home.php?q=tradecategories&col1=".$_GET['col1'].""; 
		}

	}
	else if($_GET['col2'] != '') 
	{
		if($_GET['pid'] != '') {
			$pageName = "home.php?q=tradecategories&pid=".$_GET['pid']."&col1=".$_GET['col2'].""; 
		} else {
			$pageName = "home.php?q=tradecategories&col1=".$_GET['col2'].""; 
		}

	}
	else 
	{
		
		if($_GET['pid'] != '') {
			$pageName = "home.php?q=tradecategories&pid=".$_GET['pid']." "; 
		} else {
			$pageName = "home.php?q=tradecategories"; 
		}
	}
	
	
	## Count all the records
	$cmsArray = $tradeObj->getAllTradesCategory($pid,$searchindex,$col1);
	$total_rows = count($cmsArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$tradeArray = $tradeObj->getAllTradesCategory($pid,$searchindex,$col1, $rowsPerPage,$offset);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	


	for($i=0;$i<count($tradeArray);$i++)
	{	
		if($_GET['pid']=='')
		{	
			$parentArray=$tradeObj->getCountJobtypeByTradeId($tradeArray[$i]['id']);		
			$tradeArray[$i]['cntjobtype'] =$parentArray;
		}
		$getchktrade = $leadObj->getchktradecategoryByJobID($tradeArray[$i]['id']);
		if($getchktrade['leadcnt'] > 0)
		{
			$tradeArray[$i]['used'] ='jobtype';
		}
		else
		{			
			$getAllJobTypeArray=$tradeObj->getAllJobtypeByTradeId($tradeArray[$i]['id']);		
			
			if($getAllJobTypeArray[0]['id']!='')
			{
				foreach($getAllJobTypeArray as	$getJobTypeArray)
				{
						$getchktrade = $leadObj->getchktradecategoryByJobID($getJobTypeArray['id']);
						if($getchktrade['leadcnt'] > 0)
						{
							$tradeArray[$i]['used'] ='tradecat';
						}						
				}
			}
			else
				$tradeArray[$i]['used'] ='';
		}
		
		
	}

if($col2=='1')
{
			//$order = array("trade_category DESC");
	$tradeArray = order_array_num($tradeArray,'cntjobtype','ASC');
		$tradeArray = @array_values($tradeArray);		
}
//echo "<pre>"; print_r($tradeArray);exit;
$smarty->assign('tradeArray', $tradeArray);
	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'tradecat');
$smarty->assign('mainmenu', '2');

## Unset all the objects created which are on this page
unset($tradeObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'tradecategories/tradecategories.tpl');
unset($smarty);
?>